/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Pengemudi.java
 *
 * Created on Jul 24, 2010, 9:46:59 PM
 */
package com.oi.eksekutif.ui.form;

import com.oi.eksekutif.dao.MerkMobilDAO;
import com.oi.eksekutif.domain.MerkMobil;
import com.oi.eksekutif.ui.model.MerkMobilTableModel;
import com.oi.eksekutif.util.ButtonMode;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author jasoet
 */
@Component
public class MerkMobilPanel extends javax.swing.JPanel {

    private List<MerkMobil> data;
    private MerkMobilDAO merkMobilDAO;
    private ButtonMode buttonMode = ButtonMode.NONE;
    private Validator validator;

    public Validator getValidator() {
        return validator;
    }

    @Autowired
    public void setValidator(Validator validator) {
        this.validator = validator;
    }

    /** Creates new form Pengemudi */
    public MerkMobilPanel() {
        initComponents();
    }

    public MerkMobilDAO getMerkMobilDAO() {
        return merkMobilDAO;
    }

    @Autowired
    public void setMerkMobilDAO(MerkMobilDAO merkMobilDAO) {
        this.merkMobilDAO = merkMobilDAO;
        this.data = this.merkMobilDAO.findAll();
        this.mainTable.setModel(new MerkMobilTableModel(data));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        mainTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        baruButton = new javax.swing.JButton();
        ubahButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();
        simpanButton = new javax.swing.JButton();
        merkMobilDetailForm = new com.oi.eksekutif.ui.detail.MerkMobilDetailForm();

        mainTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        mainTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mainTableMouseReleased(evt);
            }
        });
        mainTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                mainTableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(mainTable);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        baruButton.setMnemonic('B');
        baruButton.setText("Baru");
        baruButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baruButtonActionPerformed(evt);
            }
        });

        ubahButton.setMnemonic('U');
        ubahButton.setText("Ubah");
        ubahButton.setEnabled(false);
        ubahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahButtonActionPerformed(evt);
            }
        });

        hapusButton.setMnemonic('H');
        hapusButton.setText("Hapus");
        hapusButton.setEnabled(false);
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        batalButton.setMnemonic('t');
        batalButton.setText("Batal");
        batalButton.setEnabled(false);
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        simpanButton.setMnemonic('S');
        simpanButton.setText("Simpan");
        simpanButton.setEnabled(false);
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(baruButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ubahButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hapusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(batalButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simpanButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {baruButton, batalButton, hapusButton, simpanButton, ubahButton});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(baruButton)
                    .addComponent(ubahButton)
                    .addComponent(hapusButton)
                    .addComponent(batalButton)
                    .addComponent(simpanButton))
                .addContainerGap())
        );

        jPanel2.add(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 916, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 916, Short.MAX_VALUE)
                    .addComponent(merkMobilDetailForm, javax.swing.GroupLayout.DEFAULT_SIZE, 916, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(merkMobilDetailForm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                .addGap(35, 35, 35))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void mainTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mainTableKeyReleased
        MerkMobil jk = this.data.get(mainTable.getSelectedRow());
        this.merkMobilDetailForm.setMerkMobil(jk);

        this.merkMobilDetailForm.setEnabled(false);

        baruButton.setEnabled(true);
        ubahButton.setEnabled(true);
        hapusButton.setEnabled(true);

        batalButton.setEnabled(false);
        simpanButton.setEnabled(false);

        this.buttonMode = ButtonMode.NONE;
    }//GEN-LAST:event_mainTableKeyReleased

    private void mainTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainTableMouseReleased
        MerkMobil jk = this.data.get(mainTable.getSelectedRow());
        this.merkMobilDetailForm.setMerkMobil(jk);

        this.merkMobilDetailForm.setEnabled(false);

        baruButton.setEnabled(true);
        ubahButton.setEnabled(true);
        hapusButton.setEnabled(true);

        batalButton.setEnabled(false);
        simpanButton.setEnabled(false);

        this.buttonMode = ButtonMode.NONE;
    }//GEN-LAST:event_mainTableMouseReleased

    private void baruButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baruButtonActionPerformed
        MerkMobil o = new MerkMobil();
        this.merkMobilDetailForm.setMerkMobil(o);

        this.merkMobilDetailForm.setEnabled(true);

        baruButton.setEnabled(false);
        ubahButton.setEnabled(false);
        hapusButton.setEnabled(false);

        batalButton.setEnabled(true);
        simpanButton.setEnabled(true);

        this.buttonMode = ButtonMode.NEW;

    }//GEN-LAST:event_baruButtonActionPerformed

    private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
        MerkMobil o = new MerkMobil();
        this.merkMobilDetailForm.setMerkMobil(o);

        this.merkMobilDetailForm.setEnabled(false);

        baruButton.setEnabled(true);
        ubahButton.setEnabled(false);
        hapusButton.setEnabled(false);

        batalButton.setEnabled(false);
        simpanButton.setEnabled(false);

        this.buttonMode = ButtonMode.NONE;
    }//GEN-LAST:event_batalButtonActionPerformed

    private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed
        if (this.buttonMode == ButtonMode.NEW) {
            MerkMobil o = this.merkMobilDetailForm.getMerkMobil();
            Set<ConstraintViolation<MerkMobil>> violation =
                    validator.validate(o);
            if (violation.size() > 0) {
                JOptionPane.showMessageDialog(this, "Input Masih salah", "Kesalahan Input", JOptionPane.ERROR_MESSAGE);
                for (ConstraintViolation<MerkMobil> cvp : violation) {
                    System.out.println(cvp.getMessage() + " : " + cvp.getInvalidValue());
                }
                return;
            } else {
                try {
                    merkMobilDAO.persist(o);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Data sudah ada", "Kesalahan Input", JOptionPane.ERROR_MESSAGE);
                    e.printStackTrace();
                    return;
                }
                this.data = merkMobilDAO.findAll();
                this.mainTable.setModel(new MerkMobilTableModel(data));
            }
        } else if (this.buttonMode == ButtonMode.EDIT) {
            MerkMobil o = this.merkMobilDetailForm.getMerkMobil();
            Set<ConstraintViolation<MerkMobil>> violation =
                    validator.validate(o);
            if (violation.size() > 0) {
                JOptionPane.showMessageDialog(this, "Input Masih salah", "Kesalahan Input", JOptionPane.ERROR_MESSAGE);
                for (ConstraintViolation<MerkMobil> cvp : violation) {
                    System.out.println(cvp.getMessage() + " : " + cvp.getInvalidValue());
                }
                return;
            } else {
                try {
                    merkMobilDAO.merge(o);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Data sudah ada", "Kesalahan Input", JOptionPane.ERROR_MESSAGE);
                    e.printStackTrace();
                    return;
                }
                this.data = merkMobilDAO.findAll();
                this.mainTable.setModel(new MerkMobilTableModel(data));
            }
        }

        this.merkMobilDetailForm.setEnabled(false);

        baruButton.setEnabled(true);
        ubahButton.setEnabled(false);
        hapusButton.setEnabled(false);

        batalButton.setEnabled(false);
        simpanButton.setEnabled(false);

    }//GEN-LAST:event_simpanButtonActionPerformed

    private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
        MerkMobil o = data.get(mainTable.getSelectedRow());
        int confirm = JOptionPane.showConfirmDialog(this, "Apakah anda yakin Menghapus data Merk Mobil " + o.getPabrikan() + " " + o.getSeri(), "Konfirmasi", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (confirm == 0) {
            try {
                merkMobilDAO.remove(o);
            } catch (Exception e) {
                 JOptionPane.showMessageDialog(this, "Data tidak dapat dihapus, Silahkan hubungi administrator", "Gagal Delete", JOptionPane.ERROR_MESSAGE);
            }
            this.data = merkMobilDAO.findAll();
            this.mainTable.setModel(new MerkMobilTableModel(data));
            o = new MerkMobil();
        }
        this.merkMobilDetailForm.setMerkMobil(o);
        this.merkMobilDetailForm.setEnabled(false);

        baruButton.setEnabled(true);
        ubahButton.setEnabled(false);
        hapusButton.setEnabled(false);

        batalButton.setEnabled(false);
        simpanButton.setEnabled(false);

        this.buttonMode = ButtonMode.NONE;


    }//GEN-LAST:event_hapusButtonActionPerformed

    private void ubahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubahButtonActionPerformed
        MerkMobil o = data.get(mainTable.getSelectedRow());
        this.merkMobilDetailForm.setMerkMobil(o);
        this.merkMobilDetailForm.setEnabled(true);

        baruButton.setEnabled(false);
        ubahButton.setEnabled(false);
        hapusButton.setEnabled(false);

        batalButton.setEnabled(true);
        simpanButton.setEnabled(true);

        this.buttonMode = ButtonMode.EDIT;

    }//GEN-LAST:event_ubahButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton baruButton;
    private javax.swing.JButton batalButton;
    private javax.swing.JButton hapusButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable mainTable;
    private com.oi.eksekutif.ui.detail.MerkMobilDetailForm merkMobilDetailForm;
    private javax.swing.JButton simpanButton;
    private javax.swing.JButton ubahButton;
    // End of variables declaration//GEN-END:variables
}
